package com.iuie.ns.devtools.element;

import java.util.List;

import com.iuie.ns.frame.utils.StringUtils;

public class SQLSelectElement extends DevElement {
	
	private ResultMapOperationElement operationElement;
	
	public SQLSelectElement(ResultMapOperationElement operationElement) {
		this.operationElement = operationElement;
	}

	@Override
	protected String getPrefix() {
		StringBuilder sb = new StringBuilder(12);
		sb.append("\t\tselect \n\t\t\t")
			.append(this.operationElement.getSQLFieldElement().build())
			.append("\n\t\tfrom ")
			.append(this.operationElement.getHeader().getTableName())
			.append(" \n\t\twhere ");
		if (StringUtils.equals(operationElement.getOperation(), "findByCondition")) {
			sb.append("1 = 1");
		}
		return sb.toString();
	}

	@Override
	protected String getSuffix() {
		return null;
	}

	@Override
	protected List<DevElement> getChildren() {
		if (StringUtils.equals(this.operationElement.getOperation(), "findById")) {
			return this.operationElement.getPrimaryKeyEqualValueElements(false);
		} else {
			return this.operationElement.getQueryConditionElements();
		}
	}

}
